package test.jb698.codec.msm.demo;

import java.util.ArrayList;
import java.util.List;

import cn.jb698.codec.msm.generator.v5base.BcrudCfgObjCreateService;
import cn.jb698.codec.msm.generator.v5base.BcrudCfgObjCreateServiceMysqlImpl;
import cn.jb698.codec.msm.generator.v5base.BcrudCreateCodeFilesImpl;
import cn.jb698.codec.msm.generator.v5base.beans.BcrudCfgColumnObj;
import cn.jb698.codec.msm.generator.v5base.beans.BcrudCfgTableObj;
import cn.jb698.codec.msm.generator.v5base.beans.BcrudConfigBean;
import cn.jb698.codec.msm.generator.v5base.beans.FileOutputTpltCfg;
import cn.jb698.codec.msm.util.CodecMsmFileOutputTpltUtils;
import test.jb698.codec.util.TestCodecPropertiesUtils;

/**
 * Demo-复杂配置
 * @author Administrator
 * @date 2025-03-17 16:44:30
 */
public class CodecDemoSimpleConfig {

    
    /**
     * 主流程入口
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {

        // 全局配置,表级别配置初始化
        BcrudConfigBean configBean = createBdrudConfigBean();

        // 构建实现类
        BcrudCfgObjCreateService bcrudCfgObjCreateService = new BcrudCfgObjCreateServiceMysqlImpl();
        
        // 开始构建初始化表及字段信息
        List<BcrudCfgTableObj> cfgTableList = bcrudCfgObjCreateService.doBcrudCfgTableObjListCreate(configBean);
        {
            // 二次修改表及字段配置数据,覆盖默认生成的配置信息
            {
                BcrudCfgTableObj cfgTable_dt_demo_base_inf = cfgTableList.stream()
                        .filter(obj -> obj.getTableName().equals("dt_demo_base_inf")).findFirst().orElse(null);
                BcrudCfgColumnObj cfgColumn_goto = cfgTable_dt_demo_base_inf.doGetCfgColumnByColumn("goto");
                cfgColumn_goto.setPropName("myGoto"); // goto 保留字冲突属性名称变更
            }
        }

        // 文件对象自定义补充(JavaBean配置)
        BcrudCreateCodeFilesImpl bcrudCreateCodeFile = new BcrudCreateCodeFilesImpl();

        // 表配置及属性配置输出到Json文件
        bcrudCreateCodeFile.doCfgTableObjListOutputToJsonFiles(configBean, cfgTableList); // 输出表信息JSON到配置的输出目录

        // 输出构建代码文件到输出目录
        for (BcrudCfgTableObj tmpCfgTable : cfgTableList) {

            // 初始化Baomidou配套模板
            List<FileOutputTpltCfg> fileOutputTpltCfgList = CodecMsmFileOutputTpltUtils.doFileOutputTpltCfgAppend4Bmd(configBean, null);
            configBean.setFileOutputTpltCfgList(fileOutputTpltCfgList);

            // 输出模板到配置指定输出 confBean.getProjRootDir()
            bcrudCreateCodeFile.doCodeFilesCreateAndOutput(configBean, tmpCfgTable);
        }
    }

    /**
     * 初始化 全局配置,表级别配置,
     */
    private static BcrudConfigBean createBdrudConfigBean() {

        String default_driverClassName = TestCodecPropertiesUtils.getPropVal(TestCodecPropertiesUtils.propKey_driverClassName);
        String default_connectUrl = TestCodecPropertiesUtils.getPropVal(TestCodecPropertiesUtils.propKey_url);
        String default_userName = TestCodecPropertiesUtils.getPropVal(TestCodecPropertiesUtils.propKey_username);
        String default_password = TestCodecPropertiesUtils.getPropVal(TestCodecPropertiesUtils.propKey_password);
//        { // 本地自定义
//            default_driverClassName = "";
//            default_connectUrl = "";
//            default_userName = "";
//            default_password = "";
//        }
        String basePackageName = "cn.jb698.codecmsmdemo.dbcrud";
        String xmlMapperPackage = "mybatis.constructorg.dbcrud.mapper";

        String tgtProjDir = "D:/dev/workspace/codec/jb698-codec-msm-demox"; // 目标项目路径.
        String jsonFile_outDir = tgtProjDir + "/table_jsons/dbmysql";
        String codeOutDir = tgtProjDir + "/src/main/java/";
        String resOutDir = tgtProjDir + "/src/main/resources/";
        String pageOutDir = tgtProjDir + "/src/main/webapp/WEB-INF/jsp/temp_aceadmin/bcrud/";

        List<BcrudCfgTableObj> lmtCfgTables = new ArrayList<>();
        {
            // DEMO 基础表
            BcrudCfgTableObj cfgTableBean = new BcrudCfgTableObj("dt_demo_base_inf", "t");
            cfgTableBean.doHasSwaggerUi(true).doHasPersistence(true).doHasBaomidou(true); // 设置输出
            cfgTableBean.setHasPersistence(false);
            cfgTableBean.setIgnoreColumns(new String[] { "deprecate_column" }); // 淘汰的字段,保留字(待支持)
            lmtCfgTables.add(cfgTableBean);
        }
        {
            // DEMO 基础表
            BcrudCfgTableObj cfgTableBean = new BcrudCfgTableObj("dt_demo_base_ext", "t");
            cfgTableBean.doHasSwaggerUi(true).doHasPersistence(true).doHasBaomidou(true); // 设置输出
            cfgTableBean.setHasPersistence(false);
            cfgTableBean.setIgnoreColumns(new String[] { "ext_deprecate_column" }); // 淘汰的字段
            lmtCfgTables.add(cfgTableBean);
        }

        BcrudConfigBean confBean = new BcrudConfigBean();
        confBean.setDbDriverClassName(default_driverClassName);
        confBean.setDbConnectionUrl(default_connectUrl);
        confBean.setDbUserName(default_userName);
        confBean.setDbPassword(default_password);

        confBean.setBasePackageName(basePackageName);
        confBean.setJavaCodeRootDir(codeOutDir);
        confBean.setResCodeRootDir(resOutDir);
        confBean.setMapperXmlPackageName(xmlMapperPackage);
        confBean.setPageCodeRootDir(pageOutDir);
        confBean.setJsonFileOutDir(jsonFile_outDir);
        confBean.setCfgTables(lmtCfgTables);
        confBean.setProjRootDir(tgtProjDir);

        return confBean;

    }


}
